/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       connection.h
 * Author:     karooolek
 * Created on: 2009-03-14
 *
 **********************************************************************************************************************/

#ifndef CONNECTION_H_
#define CONNECTION_H_

#include "connectionstate.h"
#include "../common.h"

namespace mGameEngine
{
namespace Network
{

/**
 * Interface for connections with another machines/processes.
 */
class Connection
{
protected:
    /**
     * Current state.
     */
    volatile ConnectionState _state;

    /**
     * Timeout.
     */
    float _timeout;

public:
    /**
     * Create connection.
     */
    Connection();

    /**
     * Destroy connection.
     */
    virtual ~Connection();

    /**
     * Get connection timeout.
     * @return connection timeout in seconds.
     */
    float getTimeout() const
    {
        return _timeout;
    }

    /**
     * Set connection timeout.
     * @param timeout connection timeout in seconds.
     */
    void setTimeout(float timeout)
    {
        if(timeout < 0.0f)
        {
            return;
        }
        _timeout = timeout;
    }

    /**
     * Open connection.
     */
    virtual void open() = 0;

    /**
     * Close connection.
     */
    virtual void close() = 0;

    /**
     * Connect to connection server.
     */
    virtual void connect() = 0;

    /**
     * Disconnect from connection server.
     */
    virtual void disconnect() = 0;

    /**
     * Get connection state.
     * @return actual connection state
     */
    ConnectionState getState() const
    {
        return _state;
    }

    /**
     * Send data to another machine/process.
     * @param data data to be sent.
     * @param size data size.
     * @return number of sent bytes or 0 if connection was lost.
     */
    virtual uint send(const uint8 *data, uint size) = 0;

    /**
     * Receive data from another machine/process.
     * @param buffer buffer to store received data.
     * @param size buffer size.
     * @return number of received bytes or 0 if connection was lost.
     */
    virtual uint receive(uint8 *buffer, uint size) = 0;
};

}
}

#endif // CONNECTION_H_
